var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');

let pathsToClean = [
  'dist',
];
var isProd = process.env.NODE_ENV === 'production'; // true or false
var cssDev = ['style-loader','css-loader?sourceMap','sass-loader?sourceMap'];
var cssProd = ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: ['css-loader?sourceMap', 'sass-loader?sourceMap']
              })
var cssConfig = isProd ? cssProd : cssDev;
module.exports = {
  mode: 'development',
  entry: {
    "app.bundle": "./src/app.js",
    "contact": "./src/contact.js",
  },
  output:{
    path: path.resolve(__dirname+'/dist'),
    filename:"[name].[hash].js"
  },
  devtool: 'source-map',
  devServer:{
    port: 8080,
    open: true,
    inline:true,
    hot: true
  },
  plugins:[
    new HtmlWebpackPlugin({
      title:'hello world',
      template:'./src/index.html',
      filename:'index.html',
      hash:true,
      minify:{
        collapseWhitespace: true,
      },
      excludeChunks:['contact']
    }),
    new HtmlWebpackPlugin({
      title:'hello world',
      template:'./src/contact.html',
      filename:'contact.html',
      hash:true,
      minify:{
        collapseWhitespace: true,
      },
      chunks:['contact']
    }),
    new ExtractTextPlugin({
      filename: 'style.css',
      disable: !isProd,
      publicPath: 'css/'
    }),
    new CleanWebpackPlugin(pathsToClean),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
  ],
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: cssConfig
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [{
          loader:'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'images/'
          }
        },
          {
            loader: 'image-webpack-loader',
            options: {
              bypassOnDebug: true
            }
          }
        ]
      },
      {
        test: /\.html$/,
        use: [{
          loader: 'html-loader',
          options: {
            minimize: true
          }
        }]
      }
    ]
  },
}